// pages/getuserinfo/getuserinfo.js
const app=getApp();
Page({

  data: {
    isShow:false,
    userInfo:{},
    user_name:'',
    user_image:'',
    token:'',
  },

  onLoad(){
    let isShow=wx.getStorageSync('isShow') || false
    let userInfo=wx.getStorageSync('userInfo')
    console.log(userInfo)
    if(isShow){
      //已经授过权
      this.setData({
        isShow:true,
        userInfo
      })
    }
  },

  getuserinfo(){
    // 登录
    let that=this;
    wx.getUserProfile({
      desc: '获取用户信息',
      success(res){
        let userInfo=res.userInfo;
        wx.login({
          success: res => { 
            let code=res.code;
            // console.log(code);
            wx.request({
              url:`${app.globalData.url}/mp-login/${code}`,
              header: { 
                "content-type": 'application/json',
              },
              method: "GET",
                success:(r)=>{
                  let token=r.data.token;
                  // console.log(token);
                  that.setData({
                    isShow:true,
                    token: token
                  })
                  wx.setStorageSync('isShow', true);
                  wx.setStorageSync('userInfo', userInfo);
                  that.setData({
                    userInfo
                  })
                  wx.setStorageSync('token', token)
                  wx.request({
                    url:`${app.globalData.url}/user/info`,
                    header: { "content-type": 'application/json',
                      'authorization': token
                    },
                    method: "POST",
                    data:{
                      user_image: userInfo.avatarUrl,
                      user_name: userInfo.nickName
                    },
                      success:(r)=>{
                        that.setData({
                          isShow:true,
                          token:r.token
                        })
                        wx.setStorageSync('isShow', true);
                        wx.setStorageSync('userInfo', userInfo);
                      },
                      fail(){
                        wx.showToast({
                          title:'请求获取您的信息',
                          icon:'none'
                        })
                      }
                  })
                  
                },
                fail(){
                  wx.showToast({
                    title:'请求获取您的信息',
                    icon:'none'
                  })
                }
            })
          }
        })
      }
    })
  },
  good:function(){
    if(wx.getStorageSync("isShow") == false){
      wx.showModal({//显示模态对话框
        content: '请先登录'
      })
      return
    }
    else{
      wx.navigateTo({
      url: `/pages/good/good`,
    })
    }
  },
  collect:function(){
    if(wx.getStorageSync("isShow") == false){
      wx.showModal({//显示模态对话框
        content: '请先登录'
      })
      return
    }
    else{
      wx.navigateTo({
      url: `/pages/collect/collect`,
    })
    }
  },
})